home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WarriorsProgress.sit / Warrior’s Progress / source code / Source / Libraries / Windows / WindowObject.cp < prev    next >
Text File  |  1997-06-28  |  4KB  |  182 lines

  1. // WindowObject.cp
  2.  
  3. #ifndef WindowObject_h
  4. #include "WindowObject.h"
  5. #endif
  6. #ifndef MemoryFullError_h
  7. #include "MemoryFullError.h"
  8. #endif
  9. #ifndef GraphicsDeviceObject_h
  10. #include "GraphicsDeviceObject.h"
  11. #endif
  12.  
  13. WindowObject::WindowObject( Definition definition,
  14.                                      bool withClosebox )
  15.   {
  16.     static Rect defaultBounds = { 30, 30, 130, 130 };
  17.     
  18.     GrafPort *result = NewCWindow( reinterpret_cast<Ptr>( &port ),
  19.                                              &defaultBounds,
  20.                                              "\p",
  21.                                              false,
  22.                                              definition,
  23.                                              0,
  24.                                              withClosebox,
  25.                                              0 );
  26.     
  27.     if ( result == 0 )
  28.         throw MemoryFullError();
  29.     
  30.     Assert( result == &port );
  31.     SetPort( result );
  32.   }
  33.  
  34. PointObject WindowObject::SizeByDragging( PointObject mouse,
  35.                                                         PointObject minimum,
  36.                                                         PointObject maximum )
  37.   {
  38.     // GrowWindow doesn't let you acheive the maximum:
  39.         if ( maximum.h < maxint16 )
  40.             maximum.h++;
  41.         if ( maximum.v < maxint16 )
  42.             maximum.v++;
  43.     
  44.     Rectangle bounds( minimum.h,
  45.                             minimum.v,
  46.                             maximum.h,
  47.                             maximum.v );
  48.     
  49.     uint32 result = GrowWindow( &port, mouse, &bounds );
  50.     
  51.     if ( result == 0 )
  52.         return Size();
  53.     
  54.     return PointObject( Word0( result ), Word1( result ) );
  55.   }
  56.  
  57. void WindowObject::SetBounds( Rectangle newBounds )
  58.   {
  59.     Rectangle oldBounds( GlobalBounds() );
  60.     
  61.     if ( newBounds == oldBounds )
  62.         return;
  63.     
  64.     if ( newBounds.TopLeft() != oldBounds.TopLeft() )
  65.         SetPosition( newBounds.TopLeft() );
  66.     
  67.     if ( newBounds.Size() != oldBounds.Size() )
  68.         SetSize( newBounds.Size() );
  69.   }
  70.  
  71. void WindowObject::ZoomToUserBounds()
  72.   {
  73.     ZoomWindow( &port, inZoomIn, false );
  74.     InvalRect( &port.portRect );
  75.   }
  76.  
  77. void WindowObject::ZoomToStandardBounds()
  78.   {
  79.     ZoomWindow( &port, inZoomOut, false );
  80.     InvalRect( &port.portRect );
  81.   }
  82.  
  83. WStateData& WindowObject::StateData()
  84.   {
  85.     Assert( dataHandle != 0 );
  86.     Assert( *dataHandle != 0 );
  87.     return **reinterpret_cast<WStateData **>( dataHandle );
  88.   }
  89.  
  90. const WStateData& WindowObject::StateData() const
  91.   {
  92.     Assert( dataHandle != 0 );
  93.     Assert( *dataHandle != 0 );
  94.     return **reinterpret_cast<WStateData **>( dataHandle );
  95.   }
  96.  
  97. uint32 WindowObject::Index() const
  98.   {
  99.     Assert( Visible() );
  100.     
  101.     if ( !Visible() )
  102.         throw OSError( errAENoSuchObject );
  103.     
  104.     uint32 index = 1;
  105.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  106.       {
  107.         if ( !w->Visible() )
  108.             continue;
  109.         
  110.         if ( w == this )
  111.             return index;
  112.         
  113.         index++;
  114.       }
  115.     
  116.     Assert( 0 );
  117.     throw OSError( errAENoSuchObject );
  118.     return 0;
  119.   }
  120.  
  121. void WindowObject::SetIndex( uint32 newIndex )
  122.   {
  123.     Assert( newIndex > 0 );
  124.     if ( newIndex == 0 )
  125.         throw OSError( errAEIllegalIndex );
  126.     
  127.     if ( newIndex == 1 )
  128.         Select();
  129.      else
  130.       {
  131.         WindowObject *w = Front();
  132.         
  133.         while ( w != 0 && newIndex > 2 )
  134.           {
  135.             if ( w->Visible() )
  136.                 newIndex--;
  137.             
  138.             w = w->Next();
  139.           }    
  140.     
  141.         if ( w == 0 )
  142.             throw OSError( errAEIllegalIndex );
  143.         
  144.         SendBehind( *w );
  145.       }
  146.     
  147.     if ( !Visible() )
  148.         Show();
  149.   }
  150.  
  151. GDHandle WindowObject::NearestScreen() const
  152.   {
  153.     return GraphicsDeviceObject::Nearest( GlobalBounds() );
  154.   }
  155.  
  156. GDHandle WindowObject::DeepestScreen() const
  157.   {
  158.     return GraphicsDeviceObject::Deepest( GlobalBounds() );
  159.   }
  160.  
  161. uint32 WindowObject::CountVisibleWindows()
  162.   {
  163.     uint32 count = 0;
  164.     
  165.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  166.         if ( w->Visible()  )
  167.             count++;
  168.     
  169.     return count;
  170.   }
  171.  
  172. uint32 WindowObject::CountVisibleWindows( Rectangle target )
  173.   {
  174.     uint32 count = 0;
  175.     
  176.     for ( WindowObject *w = Front(); w != 0; w = w->Next() )
  177.         if ( w->Visible() && target.Contains( w->GlobalBounds().TopLeft() ) )
  178.             count++;
  179.     
  180.     return count;
  181.   }
  182.